package com.joe.blockview.ui;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;

import com.joe.blockview.R;
import com.joe.blockview.model.Connection;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

public class BlockIfView extends BlockView {
    public BlockIfView(Context context) {
        super(context);
    }

    public BlockIfView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public BlockIfView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        mBlockViewSize.x = selfWidth;
        mBlockViewSize.y = selfHeight;
        Connection tag = getConnection();
        nextLength = 0;
        if (tag != null) {
            tag = tag.getNextConnection();
            if (tag != null) {
                nextLength = nextLength + getBlockLength(tag.getTagBlockView())-50;
            }
            while (tag != null) {
                tag = tag.getTagConnection();
                if (tag != null) {
                    nextLength = nextLength + getBlockLength(tag.getTagBlockView());
                }
            }
        }

        mBlockViewSize.y = mBlockViewSize.y + nextLength;
        setMeasuredDimension(mBlockViewSize.x, mBlockViewSize.y);
    }



    @Override
    public void init() {
        super.init();
        connection.setType(2);
        selfHeight = 250;
        selfWidth = 465;

        setCommand("If");
        setName("If");
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onDraw(Canvas canvas) {

        paint.setAntiAlias(true);
        paint.setDither(true);
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.rgb(129, 113, 178));

        //添加路径
        Path path1 = new Path();
        path1.moveTo(10, 0);
        path1.lineTo(40, 0);
        path1.lineTo(60, 20);
        path1.lineTo(80, 20);
        path1.lineTo(100, 0);
        path1.lineTo(400, 0);
        path1.addArc(335, 0, 465, 130, -90, 180);
        path1.lineTo(400, 130);
        path1.lineTo(120, 130);
        path1.lineTo(100, 150);
        path1.lineTo(80, 150);
        path1.lineTo(60, 130);
        path1.lineTo(30, 130);
        path1.arcTo(20, 130, 40, 150, 270, -90, false);
        path1.lineTo(20, 170 + nextLength);
        path1.arcTo(20, 160 + nextLength, 40, 180 + nextLength, 180, -90, false);

        path1.lineTo(60, 180 + nextLength);
        path1.lineTo(80, 200 + nextLength);
        path1.lineTo(100, 200 + nextLength);
        path1.lineTo(120, 180 + nextLength);
        path1.lineTo(250, 180 + nextLength);
        path1.arcTo(225, 180 + nextLength, 275, 230 + nextLength, -90, 180, false);
        path1.lineTo(250, 230 + nextLength);
        path1.lineTo(100, 230 + nextLength);
        path1.lineTo(80, 250 + nextLength);
        path1.lineTo(60, 250 + nextLength);
        path1.lineTo(40, 230 + nextLength);
        path1.lineTo(10, 230 + nextLength);
        path1.arcTo(0, 210 + nextLength, 20, 230 + nextLength, 90, 90, false);
        path1.lineTo(0, 30);
        path1.arcTo(0, 0, 20, 20, 180, 90, false);

        //填充内部颜色
        canvas.drawPath(path1, paint);


        //绘制边界
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(Color.rgb(95, 82, 147));
        paint.setStrokeWidth(2);
        canvas.drawPath(path1, paint);

        paint.setColor(Color.WHITE);
        paint.setTextSize(40);
        paint.setTypeface(Typeface.DEFAULT_BOLD);
        paint.setStyle(Paint.Style.FILL);
        canvas.drawText("if", 100, 80, paint);

        Path path = new Path();
        path.moveTo(180, 65);
        path.lineTo(210, 25);
        path.lineTo(370, 25);
        path.lineTo(400, 65);
        path.lineTo(370, 105);
        path.lineTo(210, 105);
        path.lineTo(180, 65);
        paint.setColor(Color.rgb(60, 92, 169));
        canvas.drawPath(path, paint);


        //添加图标
//        Drawable d = getResources().getDrawable(R.drawable.block_end_ic);
//        Bitmap bp = drawableToBitmap(d);
//        canvas.drawBitmap(bp, 150f, 30f, paint);
//
//        Drawable d2 = getResources().getDrawable(R.drawable.ic_foward);
//        Bitmap bp2 = drawableToBitmap(d2);
//        canvas.drawBitmap(bp2, 0f, 0f, paint);
    }

    //drawable转bitmap
    private Bitmap drawableToBitmap(Drawable drawable) {
        Bitmap.Config config = Bitmap.Config.ARGB_8888;
        Bitmap bitmap = Bitmap.createBitmap(75, 75, config);// 建立对应bitmap
        Canvas canvas = new Canvas(bitmap);// 建立对应bitmap的画布
        drawable.setBounds(0, 0, 75, 75);
        drawable.draw(canvas);// 把drawable内容画到画布中
        return bitmap;
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {

    }
}
